﻿using System;
using UnityEngine;

public class TimeMgr
{
    public const long A_DAY_NANOSECOND = 864000000000;  //1day代表的tick数
    public const long A_HOUR_NANOSECOND = 36000000000;  //1h代表的tick数
    public const long A_SECOND_TO_TICK = 10000000;      //1s代表的tick数
    public const long TICKS_1970 = 621355968000000000;

    public long serverTime;             //服务器登陆的时候下发的时间 毫秒
    public float clientTime;            //客户端登陆的时候的运行时间

    public void SetServerTime(long _serverTime)
    {
        serverTime = _serverTime;
        clientTime = Time.realtimeSinceStartup;
    }
    /// <summary>
    /// 获取客户端登陆到现在运行的秒数
    /// </summary>
    public float GetClientRunTime()
    {
        return Time.realtimeSinceStartup - clientTime;
    }
    /// <summary>
    /// 单位:100纳秒，//起点时间为 0001 年 1 月 1 日 00:00:00.0000000。  服务器时间
    /// </summary>
    public long GetTickCount()
    {
        return serverTime + (long)(GetClientRunTime() * 10000000L);
    }

    /// <summary>
    /// 单位:毫秒，//起点时间为 0001 年 1 月 1 日 00:00:00.0000000。 服务器时间
    /// </summary>
    public long GetMillisecondCount()
    {
        return (long)(GetTickCount() * 0.0001);
    }
    /// <summary>
    /// 单位:秒，//起点时间为 0001 年 1 月 1 日 00:00:00.0000000。 服务器时间
    /// </summary>
    public long GetSecondCount()
    {
        return (long)(GetTickCount() * 0.0000001);
    }
    /// <summary>
    /// 单位:毫秒，//起点时间为 1970 年 1 月 1 日 00:00:00.0000000。 服务器时间
    /// </summary>
    public long GetUnixMilliSecondCount()
    {
        return (long)((GetTickCount() - TICKS_1970) * 0.0001);
    }
    /// <summary>
    /// 单位:秒，//起点时间为 1970 年 1 月 1 日 00:00:00.0000000。 服务器时间
    /// </summary>
    public long GetUnixSecondCount()
    {
        return (long)((GetTickCount() - TICKS_1970) * 0.0000001);
    }
    /// <summary>
    /// 获取两个纳秒之间相差的秒数
    /// </summary>
    /// <param name="timer1"></param>
    /// <param name="timer2"></param>
    public int GetTickDifferenceSce(long timer1, long timer2)
    {
        return (int)GetTickToSce(timer1 - timer2);
    }
    /// <summary>
    /// 纳秒转换成秒
    /// </summary>
    /// <param name="time"></param>
    public long GetTickToSce(long time)
    {
        return (long)(time * 0.0000001);
    }
    /// <summary>
    /// 秒转换成纳秒
    /// </summary>
    /// <param name="time"></param>
    public long GetSecond2Tick(long time)
    {
        return time * A_SECOND_TO_TICK;
    }

    public string GetServerTimeToString(string str)
    {
        DateTime dt = new DateTime(GetTickCount());
        return dt.ToLocalTime().ToString(str);
    }

    /// <summary>
    /// 服务器时间转换字符串 格式 yyyy-MM-dd HH:mm:ss
    /// </summary>
    public string GetServerTimeToString()
    {
        DateTime dt = new DateTime(GetTickCount());
        return dt.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss");
    }

    /// <summary>
    /// Tick转换字符串  格式为 00:00:00
    /// </summary>
    /// <param name="subTime"></param>
    public string TickToString(long ticks)
    {
        string str = "{0}:{1}:{2}";
        ticks = ticks / 10000000;
        str = string.Format(str, (ticks / 3600).ToString("D2"), (ticks % 3600 / 60).ToString("D2"), (ticks % 60).ToString("D2"));
        return str;
    }

    public string TickToHourMinSec(long ticks)
    {
        string str = "{0}小时{1}分钟{2}秒";
        ticks = ticks / 10000000;
        str = string.Format(str, (ticks / 3600).ToString("D2"), (ticks % 3600 / 60).ToString("D2"), (ticks % 60).ToString("D2"));
        return str;
    }
}